JavaScript未分類


Posted by oack7426 on 2020-06-30

Immutable 不可變

tips:
Immutable value 值是不會變的
物件 陣列 以外都是不可變的

範例的 a 雖然被改變了
但其實概念是
a = 'hello' 存在記憶體0X01
a = 'yo' 存在記憶體0X02

var a = 'hello';
a = 'yo'
console.log (a)

結果
yo

因為值不會被改變

var a = 'hello';
a.split('');
console.log(a);

結果
hello

所以要把a 的值改變就得

var a = 'hello';
a = a.split('');
console.log(a);

結果
[ 'h', 'e', 'l', 'l', 'o' ]

陣列 & 物件 值會被改變

var a = 'hello';
a = a.split('');
console.log(a); // 到這裡 a 的值成功被改變,且變為 陣列


a.reverse(); // 成功改變 a 陣列排序
console.log(a);

結果
[ 'h', 'e', 'l', 'l', 'o' ]
[ 'o', 'l', 'l', 'e', 'h' ]

#javascript







Related Posts

React 只會更新畫面中有變化的部分

React 只會更新畫面中有變化的部分

迭代陣列 forEach, for of, for in, map

迭代陣列 forEach, for of, for in, map

Visual Studio Code (VS Code) 設置全域ESLint

Visual Studio Code (VS Code) 設置全域ESLint


Comments